﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ini编辑器CS
{
    public class CardPile
    {
        public string Name = "测试卡牌组";
        public string Type = "卡牌";
        public string List;
        public DynamicImg[] img;
        public string path;
        MOD parent;
        const int Normal = 0;
        public List<Card> CardList;
        public CardPile(MOD _parent)
        {
            Console.WriteLine("正在加载牌堆");
            parent = _parent;
            path = parent.foldpath + @"CardPile\";
            Global.f1.textBox1.Text = Name;
            if (parent.filepath != null)
            {
                Global.f1.listView1.Items.Clear();
                Global.f1.listView1.BeginUpdate();   //数据更新，UI暂时挂起，直到EndUpdate绘制控件，可以有效避免闪烁并大大提高加载速度 
                Load();
                Global.f1.listView1.EndUpdate();  //结束数据处理，UI界面一次性绘制。 
                display();
                Global.f1.textBox1.Text = Name;
            }
            else
            {
                img = new DynamicImg[1];
                CardList = new List<Card>();
                img[Normal] = new DynamicImg();
            }
            Console.WriteLine("牌堆加载完成");
        }
        public void Load()
        {
            Loadini();
            LoadCard();
        }
        public void Loadini()
        {
            StreamReader sr = new StreamReader(path + "config.ini", Encoding.UTF8);
            while (sr.Peek() != -1)
            {
                if (Convert.ToChar(sr.Peek()) == '[')
                    LoadSection(sr);
                else
                    sr.ReadLine();
            }
            sr.Close();
        }
        void LoadSection(StreamReader sr)
        {
            string str = sr.ReadLine();
            if (str == "[Info]")
                LoadInfo(sr);
            if (str == "[Image]")
                LoadImage(sr);
        }
        void LoadInfo(StreamReader sr)
        {
            string s;
            while (sr.Peek() != -1)
            {
                if (Convert.ToChar(sr.Peek()) == '[')
                {
                    LoadSection(sr);
                    break;
                }
                else
                {
                    s = sr.ReadLine();
                    string[] sArray = s.Split('=');
                    if (sArray[0] == "Name")
                        Name = sArray[1];
                    else if (sArray[0] == "Type")
                        Type = sArray[1];
                    else if (sArray[0] == "List")
                        List = sArray[1];
                }
            }
        }
        void LoadImage(StreamReader sr)
        {
            img = new DynamicImg[1];
            string s;
            while (sr.Peek() != -1)
            {
                if (Convert.ToChar(sr.Peek()) == '[')
                {
                    LoadSection(sr);
                    break;
                }
                else
                {
                    s = sr.ReadLine();
                    string[] sArray = s.Split('=');
                    if (sArray[0] == "Normal")
                        img[Normal] = new DynamicImg(path + sArray[1]);
                }
            }
        }
        void LoadCard()
        {
            Card card = null;
            CardList = new List<Card>();
            string[] sArray = List.Split(',');
            foreach (string i in sArray)
            {
                if (i != null&&i!="")
                {
                    if (CardList.Count != 0)
                        card = CardList.Find(o => o.ID == i);
                    if (card == null)
                        card = new Card(this, parent.foldpath + i + @"\", i);
                    CardList.Add(card);
                    Global.f1.AddCardItem(card);
                }
            }
        }
        public void display()
        {
            Console.WriteLine(Name);
            Console.WriteLine(Type);
            Console.WriteLine(List);
        }
        public void Save()
        {
            if (Directory.Exists(path) == false)//如果不存在就创建file文件夹
            {
                Directory.CreateDirectory(path);
            }
            List = "";
            List<Card> CardList2 = new List<Card>();
            for (int i=0;i<CardList.Count;i++)
            {
                if (CardList2.Contains(CardList[i]) == false)
                    CardList2.Add(CardList[i]);
                List += CardList[i].ID;
                if (i < CardList.Count - 1)
                    List += ",";
            }
            StreamWriter sw = new StreamWriter(path + "\\config.ini", false);
            sw.WriteLine("[Info]");
            sw.WriteLine("Name=" + Name);
            sw.WriteLine("Type=" + Type);
            sw.WriteLine("List=" + List);
            sw.WriteLine("[Image]");
            sw.WriteLine("Normal=image\\"+ Path.GetFileName(img[Normal].path));
            sw.Close();//写入
            Directory.CreateDirectory(path+ "image");
            if(img[Normal].path!= path + "image\\" + Path.GetFileName(img[Normal].path))
            File.Copy(img[Normal].path, path + "image\\" + Path.GetFileName(img[Normal].path), true);
            for (int i=0;i< CardList2.Count;i++)
            {
                CardList2[i].Save();
            }
        }
    }
}
